home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.06 Jun 93 / C Shell XCMD / flApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-21  |  8.2 KB  |  342 lines  |  [TEXT/KAHL]

  1. /*****
  2.  * flApp.c
  3.  *
  4.  *    Application methods for a typical application.
  5.  *
  6.  *  Copyright © 1990 Symantec Corporation.  All rights reserved.
  7.  *
  8.  *****/
  9.  
  10. #include "flApp.h"
  11. #include "flash.h"
  12. #include "CBartender.h"
  13. #include "BGetTCLInfo.h"
  14. #include "BDisplayOutput.h"
  15. #include "size_t.h"
  16. #include "string.h"
  17.  
  18. extern    CBartender    *gBartender;
  19. extern    OSType    gSignature;
  20.  
  21. #define        kExtraMasters        4
  22. #define        kRainyDayFund        20480
  23. #define        kCriticalBalance    20480
  24. #define        kToolboxBalance        20480
  25.  
  26.  
  27. /***
  28.  * IStarterApp
  29.  *
  30.  *    Initialize the application. Your initialization method should
  31.  *    at least call the inherited method. If your application class
  32.  *    defines its own instance variables or global variables, this
  33.  *    is a good place to initialize them.
  34.  *
  35.  ***/
  36.  
  37. void flApp::IflApp(void)
  38.  
  39. {
  40.     CApplication::IApplication( kExtraMasters, kRainyDayFund, 
  41.                         kCriticalBalance, kToolboxBalance);
  42.     
  43.  
  44. /*  The parameters to IApplication are the number of times to call  
  45.     MoreMasters, the total number of bytes of heap space to reserve for                       
  46.     monitoring low memory situations, and the portion of the memory
  47.     reserve to set aside for critical operations and toolbox calls.
  48.     
  49.     Four (4) is a reasonable number of MoreMasters calls,                           
  50.     but you should determine a good number for your application                     
  51.     by observing the heap using Lightsbug,                                              
  52.     TMON, or Macsbug. Set this parameter to zero, give your                         
  53.     program a rigorous work-out, then look at the heap and count                        
  54.     how many master pointer blocks have been allocated. Master                      
  55.     pointer blocks are nonrelocatable and have a size of $100                           
  56.     (hex). You should call MoreMasters at least this many                               
  57.     times -- add a few extra just to be safe. The purpose of all                        
  58.     this preflighting is to prevent heap fragmentation. You                             
  59.     don't want the Memory Manager to call MoreMasters and                           
  60.     create a nonrelocatable block in the middle of your heap. By                        
  61.     calling MoreMasters at the very beginning of the program,                           
  62.     you ensure that these blocks are allocated in a group at the                        
  63.     bottom of the heap. 
  64.                                                                         
  65.     The memory reserve is a safeguard for handling low memory                   
  66.     conditions and is used by the GrowMemory method in                              
  67.     CApplication (check there for more comments). In general,                           
  68.     your program should never request a memory block greater                        
  69.     than this reserve size without explicitly checking in                               
  70.     advance whether there is enough free memory to satisfy the                      
  71.     the request.
  72.                                                                                     
  73.  */
  74.  
  75. }
  76.  
  77.  
  78.  
  79. /***
  80.  * SetUpFileParameters
  81.  *
  82.  *    In this routine, you specify the kinds of files your
  83.  *    application opens.
  84.  *
  85.  *
  86.  ***/
  87.  
  88. void flApp::SetUpFileParameters(void)
  89.  
  90. {
  91.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  92.  
  93.         /**
  94.          **    sfNumTypes is the number of file types
  95.          **    your application knows about.
  96.          **    sfFileTypes[] is an array of file types.
  97.          **    You can define up to 4 file types in
  98.          **    sfFileTypes[].
  99.          **
  100.          **/
  101.  
  102.     sfNumTypes = 1;
  103.     sfFileTypes[0] = 'TEXT';
  104.  
  105.         /**
  106.          **    Although it's not an instance variable,
  107.          **    this method is a good place to set the
  108.          **    gSignature global variable. Set this global
  109.          **    to your application's signature. You'll use it
  110.          **    to create a file (see CFile::CreateNew()).
  111.          **
  112.          **/
  113.  
  114.     gSignature = '????';
  115. }
  116.  
  117.  
  118. /***
  119.  * SetUpMenus 
  120.  *
  121.  * Set up menus which must be created at run time, such as a
  122.  * Font menu. You can eliminate this method if your application
  123.  * does not have any such menus.
  124.  *
  125. ***/
  126.  
  127.  void flApp::SetUpMenus()
  128.  {
  129.  
  130.   inherited::SetUpMenus();  /*  Superclass takes care of adding     
  131.                                 menus specified in a MBAR id = 1    
  132.                                 resource    
  133.                             */                          
  134.  
  135.         /* Add your code for creating run-time menus here */    
  136.  }
  137.  
  138.  
  139.  
  140. /***
  141.  * DoCommand
  142.  *
  143.  *    Your application will probably handle its own commands.
  144.  *    Remember, the command numbers from 1-1023 are reserved.
  145.  *  The file Commands.h contains all the predefined TCL
  146.  *  commands.
  147.  *
  148.  *    Be sure to call the default method, so you can get
  149.  *    the default behvior for standard commands.
  150.  *
  151.  ***/
  152. void flApp::DoCommand(long theCommand)
  153.  
  154. {
  155.     switch (theCommand) {
  156.     
  157.         /* Your commands go here */
  158.     
  159.         default:    inherited::DoCommand(theCommand);
  160.                     break;
  161.     }
  162. }
  163.  
  164.  
  165. /***
  166.  *
  167.  * UpdateMenus 
  168.  *
  169.  *   Perform menu management tasks
  170.  *
  171. ***/
  172.  
  173.  void flApp::UpdateMenus()
  174.  {
  175.   inherited::UpdateMenus();     /* Enable standard commands */      
  176.  
  177.     /* Enable the commands handled by your Application class */ 
  178.  }
  179.  
  180.  
  181. /***
  182.  * Exit
  183.  *
  184.  *    Chances are you won't need this method.
  185.  *    This is the last chance your application gets to clean up
  186.  *  things like temporary files before terminating.
  187.  *
  188.  ***/
  189.  
  190. void flApp::Exit()
  191.  
  192. {
  193.     /* your exit handler here */
  194. }
  195.  
  196.  
  197. /***
  198.  * CreateDocument
  199.  *
  200.  *    The user chose New from the File menu.
  201.  *    In this method, you need to create a document and send it
  202.  *    a NewFile() message.
  203.  *
  204.  ***/
  205.  
  206. void flApp::CreateDocument()
  207.  
  208. {
  209. }
  210.  
  211. /***
  212.  * OpenDocument
  213.  *
  214.  *    The user chose Open… from the File menu.
  215.  *    In this method you need to create a document
  216.  *    and send it an OpenFile() message.
  217.  *
  218.  *    The macSFReply is a good SFReply record that contains
  219.  *    the name and vRefNum of the file the user chose to
  220.  *    open.
  221.  *
  222.  ***/
  223.  
  224. void flApp::OpenDocument(SFReply *macSFReply)
  225.  
  226. {
  227. }
  228.  
  229. /********************************************************
  230.  * XcmdRun()
  231.  *
  232.  * Entry method.
  233.  *
  234.  *******************************************************/
  235.  
  236. void flApp::XcmdRun(void)
  237. {
  238.     XCmdPtr paramPtr;
  239.     char    returnStr[1024];
  240.     
  241.     paramPtr = (XCmdPtr)NewPtr(136L);    // Size of XCmdBlock.
  242.     GetParams(returnStr);
  243.     
  244.     FillParamPtr(paramPtr, returnStr);
  245.     FlashMain(paramPtr);
  246.     
  247.     DumpData(paramPtr->returnValue);
  248.     // A real program would dispose of paramPtr and its handles
  249.     // at this point.
  250. }
  251.  
  252. /**********************************************************
  253.  * GetParams()
  254.  *
  255.  * Get the information for input into the HyperCard parameters.
  256.  *
  257.  **********************************************************/
  258.  
  259. void flApp::GetParams(char *paramStr)
  260. {
  261.     #define DLOGinfo        601        // Resource ID for DLOG template    
  262.     
  263.     BGetTCLInfo    *theDocument = NULL;
  264.     Rect        boxDescription;
  265.     
  266.     SetRect(&boxDescription, 20, 20, 260, 60);
  267.     
  268.     theDocument = new(BGetTCLInfo);
  269.     theDocument->IBGetTCLInfo(DLOGinfo, this);
  270.     theDocument->GetInfo("CSort Input Line", paramStr);
  271.     theDocument->Dispose();
  272. }
  273.  
  274. /************************************************************
  275.  * FillParamPtr()
  276.  *
  277.  * Prepare the paramRec.
  278.  *
  279.  ************************************************************/
  280.  
  281. void flApp::FillParamPtr(XCmdPtr paramPtr, char *paramStr)
  282. {
  283.     short    count = 0;
  284.     size_t    theLength;
  285.     char    *tempPtr, *tokenPtr;
  286.     char    tokenStr[256];
  287.     
  288.     tempPtr = paramStr;
  289.     while (*tempPtr != 0x00)
  290.     {
  291.         tokenPtr = (char*)&tokenStr;
  292.         if (*tempPtr == 0x22)
  293.         {
  294.             ++tempPtr;
  295.             while (*tempPtr != 0x22)
  296.             {
  297.                 *tokenPtr = *tempPtr;
  298.                 ++tempPtr;
  299.                 ++tokenPtr;
  300.             }
  301.             ++tempPtr;
  302.             if (*tempPtr == 0x2C)
  303.                 ++tempPtr;
  304.         }
  305.         else
  306.         {
  307.             while (*tempPtr != 0x2C)
  308.             {
  309.                 *tokenPtr = *tempPtr;
  310.                 ++tempPtr;
  311.                 ++tokenPtr;
  312.             }
  313.             ++tempPtr;
  314.         }
  315.         *tokenPtr = 0x00;
  316.         theLength = strlen(tokenStr);
  317.         paramPtr->params[count] = NewHandle(theLength + 1);
  318.         strcpy((char*)*(paramPtr->params[count]), tokenStr);
  319.         ++count;
  320.     }
  321.     paramPtr->paramCount = count;
  322. }
  323.  
  324. /************************************************************
  325.  * DumpData()
  326.  *
  327.  * Display data returned by the XCMD.
  328.  *
  329.  ************************************************************/
  330.  
  331. void flApp::DumpData(Handle theData)
  332. {
  333.     BDisplayOutput    *theOutput;
  334.     
  335.     gBartender->DisableMenuBar();
  336.     theOutput = new BDisplayOutput;
  337.     theOutput->IBDisplayOutput(this, TRUE);
  338.     theOutput->DisplayRun(theData);
  339.     theOutput->Dispose();
  340. }
  341.  
  342.